var INSTITUTIONAL = 0;
var EXTRAINSTITUTIONAL = 1;

$(document).ready(function() {
    $('#labelRequiredSite').hide();
    feeShowHide();
    sopShowHide();
    expDateShowHide();
    baShowHide();
    showHideContacts();

    showMoreInfo($("#div_"), "imgInfo");
    showMoreInfo($("#div_zip"), "imgInfoZip");

});

function siteShowHide(){
    var category = $('#category_select option:selected').val();
    if(category == INSTITUTIONAL){
        $('#site_tr').show();
        $('#site_tr_info').show();
    }else{
        $('#site_tr').hide();
        $('#site_tr_info').hide();
    }
}

function ecFrecuencyOtherShowHide(){
    var isOtherSelected = $('#ecFrecuency_select option:selected').val() == 10;
    if(isOtherSelected){
        $('#ecFrecuencyOther').show();
    }else{
        $('#ecFrecuencyOther').hide();
    }
}

function timeToProvideOtherShowHide(){
    var isOtherSelected = $('#timeToProvide_select option:selected').val() == 10;
    if(isOtherSelected){
        $('#timeToProvideOther').show();
    }else{
        $('#timeToProvideOther').hide();
    }
}

function feeShowHide(){
    if($("#fee_select option:selected").text() == "Yes"){
        $('.tr_fee').removeAttr('disabled');
    }else{
        $('.tr_fee').attr('disabled', 'disabled');
    }
}

function sopShowHide(){
    if($("#sopsWritten_select option:selected").text() == "Yes"){
        $('#sopFile').removeAttr('disabled');
    }
    else{
        $('#sopFile').attr('disabled', 'disabled');
    }
}

function expDateShowHide(){
    if($("#registered_select option:selected").text() == "Yes"){
        $('#expDate').removeAttr('disabled');
    }
    else{
        $('#expDate').attr('disabled', 'disabled');
    }
}

function baShowHide(){
    if ( $("#regions") != null && $("#regions option:selected").text() == "Buenos Aires"){
        $("#regDateBA").parent().parent().show();
        $("#numberBA").parent().parent().show();
        $("#pageBA").parent().parent().show();
        $("#bookBA").parent().parent().show();
    }
    else{
        $("#regDateBA").parent().parent().hide();
        $("#regDateBA").val('');
        $("#numberBA").parent().parent().hide();
        $("#numberBA").val('');
        $("#pageBA").parent().parent().hide();
        $("#pageBA").val('');
        $("#bookBA").parent().parent().hide();
        $("#bookBA").val('');
    }

}

function validate() {
    beforeSave();
    setEmptyDates();

    $.validator.addMethod("valueNotEquals", function(value, element, arg){
        return value != arg;
    });

    $("#create")
        .validate(
        {
            rules : {
                'categoryC' : {
                    required : true,
                    valueNotEquals: "-1"
                },
                'countries.country' : "required",
                'comiteEtica.name' : "required",
                'comiteEtica.numberBA' : "number",
                'comiteEtica.pageBA' : "number",
                'comiteEtica.address.zipCode' : "number",
                'comiteEtica.address.email' : {
                    email : true
                }
            },
            messages : {
                'categoryC' : {
                    valueNotEquals: "*"
                }
            },
            errorElement : "label"
        })

    var validSite;

    if($('#siteId') != null && $('#siteId').val() == "" && $("#category_select :selected").val() != 1){
        $('#searchSite').css("border-color", "red");
        $('#labelRequiredSite').show();
        validSite = false;
    }
    else{
        $('#labelRequiredSite').hide();
        $('#searchSite').css("border", "2px inset rgb(0, 0, 0)");
        validSite =  true;
    }

    return ($("#create").valid() && validSite)
}

function addRowCommitee(tableID) {

    var table = document.getElementById(tableID);

    var rowCount = table.rows.length;
    if(rowCount == 2 && table.rows[1].style.display == "none"){
        var row = table.rows[1];
        row.children[0].children[0].value = "";
        row.children[1].children[0].value = "";
        row.children[2].children[0].value = "";
        row.children[3].children[0].value = "";
        row.children[4].children[0].value = "";
        row.children[5].children[0].value = "";
        table.rows[1].style.display = "";
        return;
    }

    var counts = rowCount -1;

    var rowContact = $('#table_content tbody>tr:last').clone()[0];
    rowContact.children[0].children[0].value = '';
    rowContact.children[0].children[0].name = 'comiteEtica.contactPersons['+ counts +'].name';
    rowContact.children[1].children[0].value = '';
    rowContact.children[1].children[0].name = 'comiteEtica.contactPersons['+ counts +'].surname';
    rowContact.children[2].children[0].value = '';
    rowContact.children[2].children[0].name = 'comiteEtica.contactPersons['+ counts +'].hours';
    rowContact.children[3].children[0].value = '';
    rowContact.children[3].children[0].name = 'comiteEtica.contactPersons['+ counts +'].email';
    rowContact.children[4].children[0].value = '';
    rowContact.children[4].children[0].name = 'comiteEtica.contactPersons['+ counts +'].fax';
    rowContact.children[5].children[0].value = '';
    rowContact.children[5].children[0].name = 'comiteEtica.contactPersons['+ counts +'].phone';
    rowContact.children[6].children[0].onclick = function(){deleteRow('table_content',this); return false;};

    $('#table_content > tbody:last').append(rowContact);
}

function deleteRow(tableID, button) {

    var table = document.getElementById(tableID);
    if(table.rows.length == 2){
        table.rows[1].style.display = "none";
    }else{
        table.deleteRow(button.parentNode.parentNode.rowIndex);
    }
}

function deleteContact(tableId, fila){
    var table = document.getElementById(tableId);
    var cant = document.getElementById("cantContact");

    var rows = table.getElementsByTagName('tr');
    var size = rows.length;
    for(var j = 0; j < rows.length; j++ ){
        if(rows[j].id == fila[0].id && rows[j].cells[1].id == fila[0].cells[1].id){
            break;
        }
    }

    if(cant.value == 1){
        var borrados = 0;
        var x = j - 1;
        while(borrados < 9){
            if(rows[x].cells[1] != null){
                rows[x].cells[1].children[0].value="";
                borrados++;
            }
            x--;
        }
        return;
    }

    var borrados = 0;
    var x = j;
    while(borrados < 10){
        rows[x].parentNode.removeChild(rows[x]);
        if(rows[x].cells[1] != null){
            borrados++;
        }
        x--;
    }

    var val = parseInt(cant.value,10) - parseInt(1,10);
    cant.value = val;
}

function addContact(tableID){
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;

    var cant = document.getElementById("cantContact");
    var val = parseInt(cant.value,10) + parseInt(1,10);
    cant.value = val;

    var ultimoId = document.getElementById("ultimoIdContact");

    for(var k = ultimoId.value; k>0; k--){
        if (document.getElementById("delContactCell" + k) != null)
            break;
    }

    var newId = parseInt(ultimoId.value,10) + parseInt(1,10);
    ultimoId.value = newId;

    var idAfter = '#delContactCell' + k;

    var row8 = $('#delContactCell' + k).parent().clone()[0];
    row8.cells[0].id="delContactCell"+newId;
    row8.cssClass="trNuevoDocumento";
    $(idAfter).parent().after(row8);

    var row7 = $('#timeEnvelopes' + k).parent().parent().clone()[0];
    row7.id="role" + newId + "timeEnvelopes";
    row7.cells[1].children[0].name="comiteEtica.contactPersons[" + newId + "].timeEnvelopes";
    row7.cells[1].children[0].value="";
    row7.cells[1].children[0].id="timeEnvelopes" + newId;
    $(row7).attr('disabled', 'disabled');
    $(idAfter).parent().after(row7);

    var row6b = $('#otherDegree' + k).parent().parent().clone()[0];
    row6b.id="degree" + newId + "other";
    row6b.cells[1].children[0].name="comiteEtica.contactPersons["+newId+"].otherDegree";
    row6b.cells[1].children[0].value="";
    row6b.cells[1].children[0].id="otherDegree" + newId;
    $(row6b).hide();
    $(idAfter).parent().after(row6b);

    var row6 = $('#degree' + k).parent().parent().clone()[0];
    row6.id="role" + newId + "degree";
    row6.cells[1].children[0].name="comiteEtica.contactPersons[" + newId + "].degree.id";
    row6.cells[1].children[0].value="";
    row6.cells[1].children[0].id="degree" + newId;
    $(row6).attr('disabled', 'disabled');
    $(idAfter).parent().after(row6);

    var row5 = $('#hours' + k).parent().parent().clone()[0];
    row5.cells[1].children[0].name="comiteEtica.contactPersons[" + newId + "].hours";
    row5.cells[1].children[0].value="";
    row5.cells[1].children[0].id="hours" + newId;
    $(idAfter).parent().after(row5);

    var row4 = $('#email' + k).parent().parent().clone()[0];
    row4.cells[1].children[0].name="comiteEtica.contactPersons[" + newId + "].email";
    row4.cells[1].children[0].value=$("#emailComite").val();
    row4.cells[1].children[0].id="email" + newId;
    $(idAfter).parent().after(row4);

    var row3 = $('#fax' + k).parent().parent().clone()[0];
    row3.cells[1].children[0].name="comiteEtica.contactPersons[" + newId + "].fax";
    row3.cells[1].children[0].value=$("#faxComite").val();
    row3.cells[1].children[0].id="fax" + newId;
    $(idAfter).parent().after(row3);

    var row2 = $('#phone' + k).parent().parent().clone()[0];
    row2.cells[1].children[0].name="comiteEtica.contactPersons[" + newId + "].phone";
    row2.cells[1].children[0].value=$("#phoneComite").val();
    row2.cells[1].children[0].id="phone" + newId;
    $(idAfter).parent().after(row2);

    var row1b = $('#otherRoleInCommittee' + k).parent().parent().clone()[0];
    row1b.id="role"+newId+"Other";
    row1b.cells[1].children[0].name="comiteEtica.contactPersons[" + newId + "].otherRoleInCommittee";
    row1b.cells[1].children[0].value="";
    row1b.cells[1].children[0].id="otherRoleInCommittee" + newId;
    $(row1b).hide();
    $(idAfter).parent().after(row1b);

    var row1 = $('#role' + k).parent().parent().clone()[0];
    row1.cells[1].children[0].name="rolesInCommittee[" + newId + "]";
    row1.cells[1].children[0].value="";
    row1.cells[1].children[0].id="role" + newId;
    $(idAfter).parent().after(row1);

    showMoreInfo($("#div_"), "imgInfo");
}

function chairmanShowHide(select){
    var id= select.id;
    var idDegree= "#" + id + "degree";
    var idTime= "#" + id + "timeEnvelopes";
    if( $("#" +id + " option:selected").text() == "Chairman"){
        $($($(idDegree).children()[1]).children()[0]).removeAttr('disabled');
        $($($(idTime).children()[1]).children()[0]).removeAttr('disabled');
    }
    else{
        $($($(idDegree).children()[1]).children()[0]).attr('disabled', 'disabled');
        $($($(idDegree).children()[1]).children()[0]).val('');

        $($($(idTime).children()[1]).children()[0]).attr('disabled', 'disabled');
        $($($(idTime).children()[1]).children()[0]).val('');
    }
}

function otherShowHide(select){
    var id= select.id;
    var idSelect = "#" +id + " option:selected";
    var idOther = "#" + id + "Other";
    if( $(idSelect).text() == "Other"){
        $(idOther).show();
    }
    else{
        $(idOther).hide();
        $($($(idOther).children()[1]).children()[0]).val('');
    }
}

function automaticPhoneFaxEmail(){
    $(".phoneClass").each(function(i,v){
        if($(v).val() == ""){
            $(v).val($("#phoneComite").val());
        }
    });
    $(".emailClass").each(function(i,v){
        if($(v).val() == ""){
            $(v).val($("#emailComite").val());
        }
    });
    $(".faxClass").each(function(i,v){
        if($(v).val() == ""){
            $(v).val($("#faxComite").val());
        }
    });
}

function showHideContacts(){
    $(".roleClass").each(function(i,v){
        if($(v).val() == "Chairman"){
            $($(".degreeClass")[i]).removeAttr('disabled');
            $($(".envelopeClass")[i]).removeAttr('disabled');
            otherShowHide($(".degreeClass")[i]);
        }
        else{
            $($(".degreeClass")[i]).attr('disabled', 'disabled');
            $($(".envelopeClass")[i]).attr('disabled', 'disabled');
            otherShowHide(v);
        }
    });
}
